package com.xiebishe.mylibrary.common.glide;

import android.graphics.Bitmap;
import android.os.Environment;
import android.os.Parcel;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class FileUtils {
    private static final int BUFF_SIZE = 1048576;
    public static final long KB = 1024L;
    public static final long MB = 1048576L;
    public static final long GB = 1073741824L;

    public FileUtils() {
    }

    public static String geFileMimeType(String filename) {
        FileNameMap filenameMap = URLConnection.getFileNameMap();
        String contentType = filenameMap.getContentTypeFor(filename);
        if (contentType == null) {
            contentType = "application/octet-stream";
        }

        return contentType;
    }

    public static String getFileName(String path) {
        String fifeName = "";
        if (!TextUtils.isEmpty(path)) {
            File file = new File(path);
            if (file.exists()) {
                fifeName = file.getName();
            }
        }

        return fifeName;
    }

    public static String getFileType(String path) {
        String fifeType = "";
        if (!TextUtils.isEmpty(path)) {
            fifeType = path.substring(path.lastIndexOf(".") + 1, path.length());
        }

        return fifeType;
    }

    public static long getFileOrFolderSize(String filePath) {
        if (filePath != null) {
            File file = new File(filePath);
            return getFileOrFolderSize(file);
        } else {
            return -1L;
        }
    }

    public static long getFileOrFolderSize(@Nullable File file) {
        if (file == null) {
            return 0L;
        } else if (!file.exists()) {
            return 0L;
        } else if (file.isFile()) {
            return file.length();
        } else {
            long size = 0L;

            try {
                File[] fileList = file.listFiles();

                for(int i = 0; i < fileList.length; ++i) {
                    if (fileList[i].isDirectory()) {
                        size += getFileOrFolderSize(fileList[i]);
                    } else {
                        size += fileList[i].length();
                    }
                }
            } catch (Exception var5) {
                var5.printStackTrace();
            }

            return size;
        }
    }

    public static boolean isSDCardExsit() {
        String state = Environment.getExternalStorageState();
        return "mounted".equals(state);
    }

    public static boolean deleteFileOrFolder(String filePath, boolean deleteThisPath) {
        if (filePath != null) {
            File file = new File(filePath);
            return deleteFileOrFolder(file, deleteThisPath);
        } else {
            return false;
        }
    }

    public static boolean deleteFileOrFolder(@Nullable File file, boolean deleteThisPath) {
        if (file == null) {
            return false;
        } else if (!file.exists()) {
            return true;
        } else {
            deleteFileOrFolder(file);
            if (deleteThisPath) {
                file.delete();
            }

            return true;
        }
    }

    private static void deleteFileOrFolder(File file) {
        String[] fileList = file.list();
        if (file.isFile() || fileList != null && fileList.length == 0) {
            file.delete();
        } else {
            File[] files = file.listFiles();
            if (files != null) {
                File[] var3 = files;
                int var4 = files.length;

                for(int var5 = 0; var5 < var4; ++var5) {
                    File value = var3[var5];
                    deleteFileOrFolder(value);
                    value.delete();
                }
            }
        }

    }

    public static boolean saveBytes(byte[] bytes, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;

        boolean ss;
        try {
            File dir = new File(filePath);
            ss = dir.isDirectory();
            if (!dir.exists()) {
                dir.mkdirs();
            }

            file = new File(filePath + "//" + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
            bos.flush();
            boolean var8 = true;
            return var8;
        } catch (Exception var22) {
            var22.printStackTrace();
            ss = false;
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException var21) {
                    var21.printStackTrace();
                }
            }

            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException var20) {
                    var20.printStackTrace();
                }
            }

        }

        return ss;
    }

    public static boolean saveBitmap(Bitmap bm, String path, String picName) {
        File f = new File(path, "/" + picName);
        if (f.exists()) {
            f.delete();
        }

        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
            return true;
        } catch (FileNotFoundException var5) {
            var5.printStackTrace();
        } catch (IOException var6) {
            var6.printStackTrace();
        }

        return false;
    }

    public static File saveBitmapToAlbum(Bitmap bm, String path, int quality, String picName) {
        File f = new File(path, "/" + picName);
        if (f.exists()) {
            f.delete();
        }

        try {
            if (bm == null || bm.isRecycled()) {
                return null;
            }

            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, quality, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException var6) {
            var6.printStackTrace();
        } catch (IOException var7) {
            var7.printStackTrace();
        } catch (IllegalStateException var8) {
            var8.printStackTrace();
        }

        return f;
    }

    public static String readToString(String fileName) {
        String encoding = "UTF-8";
        File file = new File(fileName);
        Long filelength = file.length();
        byte[] filecontent = new byte[filelength.intValue()];

        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
        } catch (FileNotFoundException var7) {
            var7.printStackTrace();
        } catch (IOException var8) {
            var8.printStackTrace();
        }

        try {
            return new String(filecontent, encoding);
        } catch (UnsupportedEncodingException var6) {
            System.err.println("The OS does not support " + encoding);
            var6.printStackTrace();
            return null;
        }
    }

    public static String formatFileSize(long size) {
        if (size <= 0L) {
            return "";
        } else if (size >= 1073741824L) {
            return String.format("%.1fGB", (float)size / 1.07374182E9F);
        } else {
            float value;
            if (size >= 1048576L) {
                value = (float)size / 1048576.0F;
                return String.format("%.1fMB", value);
            } else if (size >= 1024L) {
                value = (float)size / 1024.0F;
                return String.format("%.1fKB", value);
            } else {
                return String.format("%dB", size);
            }
        }
    }

    public static void savaParcelToFile(Parcel parcel, File file) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            parcel.setDataPosition(0);
            byte[] marshall = parcel.marshall();
            fileOutputStream.write(marshall);
            fileOutputStream.close();
        } catch (FileNotFoundException var4) {
            var4.printStackTrace();
        } catch (IOException var5) {
            var5.printStackTrace();
        }

    }

    public static Parcel readParcelFromFile(File file) {
        try {
            int length = (int)file.length() + 10;
            byte[] bytes = new byte[length];
            FileInputStream fileInputStream = new FileInputStream(file);
            int allRead = 0;

            while(true) {
                int read = fileInputStream.read(bytes, allRead, length - allRead);
                if (read < 0) {
                    fileInputStream.close();
                    Parcel obtain = Parcel.obtain();
                    obtain.unmarshall(bytes, 0, allRead);
                    obtain.setDataPosition(0);
                    return obtain;
                }

                allRead += read;
            }
        } catch (FileNotFoundException var6) {
            var6.printStackTrace();
        } catch (IOException var7) {
            var7.printStackTrace();
        }

        return null;
    }

    public static void saveObjectToFile(Serializable object, File file) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(object);
            fileOutputStream.close();
        } catch (FileNotFoundException var4) {
            var4.printStackTrace();
        } catch (IOException var5) {
            var5.printStackTrace();
        }

    }

    public static <T> T readFileObjectFromFile(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            Object o = objectInputStream.readObject();
            return (T) o;
        } catch (FileNotFoundException var4) {
            var4.printStackTrace();
        } catch (IOException var5) {
            var5.printStackTrace();
        } catch (ClassNotFoundException var6) {
            var6.printStackTrace();
        }

        return null;
    }

    public static class Zip {
        public Zip() {
        }

        public static boolean decompZipFile(InputStream inputStream, String folderPath) {
            return inputStream == null ? false : decompZipInputstream(inputStream, folderPath, "", true);
        }

        public static boolean decompZipInputstream(InputStream inputStream, String folderPath, String patternStr, boolean overRide) {
            if (inputStream == null) {
                return false;
            } else {
                ZipInputStream Zin = new ZipInputStream(inputStream);
                Pattern pattern = Pattern.compile(patternStr);
                Matcher matcher = null;
                FileOutputStream out = null;

                try {
                    ZipEntry entry;
                    while((entry = Zin.getNextEntry()) != null) {
                        File desFile = new File(folderPath, entry.getName());
                        if (entry.getName().endsWith(File.separator)) {
                            if (!desFile.isDirectory()) {
                                desFile.delete();
                            }

                            desFile.mkdirs();
                        } else {
                            if (!desFile.exists()) {
                                File fileParentDir = desFile.getParentFile();
                                if (!fileParentDir.exists()) {
                                    fileParentDir.mkdirs();
                                }
                            }

                            matcher = pattern.matcher(entry.getName());
                            if ((desFile.exists() && overRide || !desFile.exists()) && matcher.find()) {
                                desFile.createNewFile();
                                out = new FileOutputStream(desFile);
                                byte[] buffer = new byte[1048576];

                                int realLength;
                                while((realLength = Zin.read(buffer)) > 0) {
                                    out.write(buffer, 0, realLength);
                                }

                                out.flush();
                            }
                        }
                    }

                    boolean var22 = true;
                    return var22;
                } catch (IOException var20) {
                    var20.printStackTrace();
                } finally {
                    try {
                        Zin.close();
                        if (out != null) {
                            out.close();
                        }
                    } catch (IOException var19) {
                        var19.printStackTrace();
                    }

                }

                return false;
            }
        }
    }

}

